package com.hjq.mallproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hjq.mallproduct.entity.vo.AttrRelationVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjq.common.utils.PageUtils;
import com.hjq.common.utils.Query;

import com.hjq.mallproduct.dao.AttrAttrgroupRelationDao;
import com.hjq.mallproduct.entity.AttrAttrgroupRelationEntity;
import com.hjq.mallproduct.service.AttrAttrgroupRelationService;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveByAttrIdAndGroupId(Long attrId, Long attrGroupId) {
        AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
        relationEntity.setAttrId(attrId);
        relationEntity.setAttrGroupId(attrGroupId);
        this.save(relationEntity);

    }

    @Override
    public void updateByAttrIdAndGroupId(Long attrId, Long attrGroupId) {
        AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
        relationEntity.setAttrId(attrId);
        relationEntity.setAttrGroupId(attrGroupId);
        baseMapper.updateByAttrId(attrId,attrGroupId);

    }

    @Override
    public void saveAttrRelationBatch(List<AttrRelationVO> attrRelationVOList) {
        if(attrRelationVOList.size()==0){
            return;
        }
        List<AttrAttrgroupRelationEntity> relationEntities = attrRelationVOList.stream()
                .map((item) -> {
            AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(item, relationEntity);
            return relationEntity;
        })
                .collect(Collectors.toList());

        baseMapper.saveAttrAttrgroupRelationBatch(relationEntities);
    }

}